package com.kitty.game.pet.handler.upgrade;

import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.pet.message.RespPetEclosionResult;
import com.kitty.mina.message.MessagePusher;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;

@Component
public class OpenEclosionPetHandler extends UpgradePetHandler {

    private static final String SUCCESS_MSG = "恭喜，你的#Y{0}#n成功开启羽化，获得#R3000#n灵气，当前羽化进度{1}#R{2}%#n。";


    @Override
    public String acceptUpgradePet(Role role, Pet pet, String ids, String itemPos, String costType, String otherPetPos) {
        if (pet.getEclosion() == 2 || pet.getEclosion() == 1) {
            return MessageFormat.format(ERROR_OPER_MSG, "羽化");
        }
        if (pet.getEnchant() != 2) {
            return NOT_ENCHANT;
        }
        int limitNum = 0;//限制物品数量
        RoleEquip roleEquip = equipService.getRoleEquipByPos(role, Short.parseShort(itemPos));
        if (roleEquip == null) {
            return ERROR_ITEM;
        }
        if (!roleEquip.getName().equals("羽化丹")) {
            return ERROR_ITEM;
        }
        if (roleEquip.isLimit()) {
            limitNum++;
        }
        petService.addLimitTime(pet, limitNum);
        equipService.delEquipNum(role, roleEquip, 1);
        int maxNimbus = getEclosionMaxNimbus(pet);
        String resultMessage;
        pet.setEclosion_k(maxNimbus);
        if (maxNimbus < 3000) {
            setEclosionValue(pet);
            pet.setEclosion((byte) 2);
            pet.setEclosion_rank((byte) 1);
            pet.setEclosion_v(0);
            pet.setEclosion_k(0);
            resultMessage = MessageFormat.format("恭喜，你的#Y{0}#n羽化初阶完成了。", pet.getName());
        } else {
            pet.setEclosion((byte) 1);
            pet.setEclosion_v(3000);
            double percent = (double) pet.getEclosion_v() / (double) pet.getEclosion_k();
            String percentMsg = "%.2f";
            percentMsg = String.format(percentMsg, (percent * 100));
            resultMessage = MessageFormat.format(SUCCESS_MSG, pet.getName(),ECLOSION_RANK.get(pet.getEclosion_rank()), percentMsg);
        }
        roleService.addAchieveScore(role,"201039");
        petService.refreshPetInfo(role, pet);
        refreshPetField(role, pet);

        RespPetEclosionResult RespPetEclosionResult = new RespPetEclosionResult();
        RespPetEclosionResult.setResult((byte) 1);
        MessagePusher.pushMessage(role, RespPetEclosionResult);
        return resultMessage;
    }
}
